package cn.iocoder.yudao.module.tms.controller.admin.companybaseinfo;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tms.controller.admin.companybaseinfo.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.companybaseinfo.CompanyBaseInfoDO;
import cn.iocoder.yudao.module.tms.service.companybaseinfo.CompanyBaseInfoService;

@Tag(name = "管理后台 - 公司基本信息")
@RestController
@RequestMapping("/tms/company-base-info")
@Validated
public class CompanyBaseInfoController {

    @Resource
    private CompanyBaseInfoService companyBaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建公司基本信息")
    @PreAuthorize("@ss.hasPermission('tms:company-base-info:create')")
    public CommonResult<Long> createCompanyBaseInfo(@Valid @RequestBody CompanyBaseInfoSaveReqVO createReqVO) {
        return success(companyBaseInfoService.createCompanyBaseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司基本信息")
    @PreAuthorize("@ss.hasPermission('tms:company-base-info:update')")
    public CommonResult<Boolean> updateCompanyBaseInfo(@Valid @RequestBody CompanyBaseInfoSaveReqVO updateReqVO) {
        companyBaseInfoService.updateCompanyBaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:company-base-info:delete')")
    public CommonResult<Boolean> deleteCompanyBaseInfo(@RequestParam("id") Long id) {
        companyBaseInfoService.deleteCompanyBaseInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公司基本信息")
                @PreAuthorize("@ss.hasPermission('tms:company-base-info:delete')")
    public CommonResult<Boolean> deleteCompanyBaseInfoList(@RequestParam("ids") List<Long> ids) {
        companyBaseInfoService.deleteCompanyBaseInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:company-base-info:query')")
    public CommonResult<CompanyBaseInfoRespVO> getCompanyBaseInfo(@RequestParam("id") Long id) {
        CompanyBaseInfoDO companyBaseInfo = companyBaseInfoService.getCompanyBaseInfo(id);
        return success(BeanUtils.toBean(companyBaseInfo, CompanyBaseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司基本信息分页")
    @PreAuthorize("@ss.hasPermission('tms:company-base-info:query')")
    public CommonResult<PageResult<CompanyBaseInfoRespVO>> getCompanyBaseInfoPage(@Valid CompanyBaseInfoPageReqVO pageReqVO) {
        PageResult<CompanyBaseInfoDO> pageResult = companyBaseInfoService.getCompanyBaseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyBaseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('tms:company-base-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyBaseInfoExcel(@Valid CompanyBaseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyBaseInfoDO> list = companyBaseInfoService.getCompanyBaseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司基本信息.xls", "数据", CompanyBaseInfoRespVO.class,
                        BeanUtils.toBean(list, CompanyBaseInfoRespVO.class));
    }

}